﻿using Raven;
using UnityEngine;
using Vector2 = Raven.Vector2;

public class HealthDisplay : UnityDisplayObject
{
    private TriggerHealthGiver _trigger;

    public HealthDisplay(BaseGameEntity entity) : base(entity)
    {
        _trigger = entity as TriggerHealthGiver;
    }

    public override void Start()
    {
        base.Start();

        Vector2 pos = _trigger.Pos();
        _gameObject.transform.position = new Vector3((float) pos.x, (float) pos.y);

        const float sz = 5;
        VertexBuffer vb = VertexBuffer.Begin();
        vb.vertexColor = Color.black;
        Rect rect = new Rect(-sz, -sz, sz * 2, sz * 2);
        vb.AddRect(rect);

        vb.vertexColor = Color.red;
        Vector3 a = new Vector3(0, -sz);
        Vector3 b = a + new Vector3(0, sz * 2);
        vb.AddLine(a, b, Vector3.forward);
        a = new Vector3(0 - sz, 0);
        b = a + new Vector3(sz * 2, 0);
        vb.AddLine(a, b, Vector3.forward);

        Mesh mesh = vb.ToMesh();
        vb.End();

        _gameObject.AddComponent<MeshFilter>().mesh = mesh;
        MeshRenderer render = _gameObject.AddComponent<MeshRenderer>();
        render.material = new Material(Shader.Find("Sprites/Default"));
    }

    public override void Update()
    {
        _gameObject.SetActive(_trigger.isActive());
    }
}